import time

import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

import TalabatConfig

chrome_driver_instance = None

deliveryTimes = []


def getDatabaseConnection():
    mysqlConnection = pymysql.connect(
        host=TalabatConfig.talabatConfig['talabat']['db']['host'],
        port=TalabatConfig.talabatConfig['talabat']['db']['port'],
        user=TalabatConfig.talabatConfig['talabat']['db']['user'],
        password=TalabatConfig.talabatConfig['talabat']['db']['pass'],
        database=TalabatConfig.talabatConfig['talabat']['db']['dbName'])

    return mysqlConnection


def insert(restaurant, rating, url, address, deliverTime):
    try:
        _connection = getDatabaseConnection()

        _cursor = _connection.cursor()

        _cursor.execute("insert into talabat_office_restaurants(restaurant, rating, url, address, delivery_time) "
                        "values ('"
                        + restaurant + "', " + str(rating) + ", '" + url + "', '"
                        + address + "', '" + deliverTime + "')")

        _connection.commit()

    except Exception as e:
        print(e.__str__())

        if _connection:
            _connection.rollback()
    finally:
        if _cursor:
            _cursor.close()
        if _connection:
            _connection.close()


def createChromeDriver():
    global chrome_driver_instance

    if chrome_driver_instance:
        return

    try:
        s = Service(executable_path='/Users/gilbert/chromedriver-mac-x64/chromedriver')
        chrome_driver_instance = webdriver.Chrome(service=s)

        chrome_driver_instance.maximize_window()
    except Exception as e:
        print(e.__str__())


def closeChromeDriver():
    chrome_driver_instance.close()


def scrapyRestaurantRating():
    createChromeDriver()

    page = int(TalabatConfig.talabatConfig['talabat']['startPage'])

    endPage = int(TalabatConfig.talabatConfig['talabat']['endPage'])

    while True:
        if page > endPage:
            break

        print("当前爬取页：" + str(page))

        actualRestaurants = traversePage(page)

        filterRestaurants(actualRestaurants)

        if len(actualRestaurants) < int(TalabatConfig.talabatConfig['talabat']['page4Items']):
            break

        page = page + 1

    closeChromeDriver()


def traversePage(page):
    global deliveryTimes

    region = TalabatConfig.talabatConfig['talabat']['v2']['region']

    chrome_driver_instance.get(region + "&page=" + str(page))

    time.sleep(4)

    restaurants = []

    deliveryTimes = []

    urls = chrome_driver_instance.find_elements(By.XPATH, "//a[@data-testid='restaurant-a']")
    for url in urls:
        restaurant = url.get_attribute('href')
        restaurants.append(restaurant)

    labels = chrome_driver_instance.find_elements(By.XPATH, "//div[@class='vendor-card']/div[@class='content']//span")
    for label in labels:
        if "mins" in label.text:
            deliveryTimes.append(label.text)

    return restaurants


def filterRestaurants(actualRestaurants):
    global deliveryTimes
    i = 0
    for restaurantUrl in actualRestaurants:
        chrome_driver_instance.get(restaurantUrl)
        time.sleep(4)

        try:
            url = chrome_driver_instance.find_element(By.XPATH, "//a[@data-testid='branch-name']").get_attribute('href')

            address = chrome_driver_instance.find_element(By.XPATH, "//h1[@data-testid='restaurant-title']").text
            address = address.replace("'", "\\'").replace("\n", "\\n")

            chrome_driver_instance.get(url)

            time.sleep(4)

            rating = chrome_driver_instance.find_element(By.XPATH, "//div[@data-testid='brand-rating-number']").text
            restaurant = chrome_driver_instance.find_element(By.XPATH, "//h1[@data-testid='brand-name']").text
            restaurant = restaurant.replace("'", "\\'").replace("\n", "\\n")

            print(restaurant + " " + str(rating) + " " + url + " " + address + " " + deliveryTimes[i])

            insert(restaurant, float(rating), url, address, deliveryTimes[i])

            i = i + 1

        except Exception as e:
            pass


if __name__ == '__main__':
    scrapyRestaurantRating()
